package com.glory.gloryquickui.view

import android.content.Context
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.edit.ClearEditText

/**
 *多行输入框
 */
class ManyEditTextController(context: Context?,val viewModel: QuickUIViewModel) : ViewController<FieldBean?>(context) {
    private var tvManyEditName: TextView? = null
    private var tvShowLimit: TextView? = null
    private var tvManyEditValue: ClearEditText? = null
    override fun resLayoutId(): Int {
        return R.layout.layout_many_edittext
    }

    override fun onBindView(data: FieldBean?) {
        tvManyEditName!!.text = data?.config?.label
        tvManyEditValue!!.hint = data?.placeholder
        tvManyEditValue!!.maxLines = data?.autosize?.maxRows!!
        tvManyEditValue!!.minLines = data.autosize.minRows
        if (data.config.defaultValue!=null&&data.config.defaultValue is String) {
            tvManyEditValue!!.setText(data.config.defaultValue.toString())
        }
        //设置可禁止
        tvManyEditValue!!.isEnabled = !data?.disabled!!
        if (data.readonly){
            tvManyEditValue!!.inputType = InputType.TYPE_NULL
        }
        //设置前后图标
        tvManyEditValue?.setLeftAndRightIcon(data.prefixIcon,data.suffixIcon)

        //设置最大字符
        tvManyEditValue?.setMaxLength(data.maxlength.toString())

        //设置字符统计
        tvShowLimit?.visibility = if (data.showWordLimit) View.VISIBLE else View.GONE
        tvShowLimit?.text = "0/" + data.maxlength.toString()
        tvManyEditValue?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                tvShowLimit?.text = tvManyEditValue?.text.toString().length.toString() + "/" + data.maxlength.toString()
                if (!data.readonly) {
                    val map = viewModel.pageData.value!!.toMutableMap()
                    map[data.vModel] = p0.toString()
                    viewModel.pageData.value = map
                    if (p1 == 0 && p2 == 0 && p3 > 1) {
                        if (p0.toString().isNotEmpty() && data.slot.append == "1") {
                            viewModel.searchData()
                        }
                    }
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }

        })
    }


    override fun onCreatedView(view: View?) {
        tvManyEditName = view?.findViewById(R.id.tvManyEditName)
        tvManyEditValue = view?.findViewById(R.id.tvManyEditValue)
        tvShowLimit = view?.findViewById(R.id.tvShowLimit)
    }

    override fun masterPart(): View? {
        return tvManyEditValue
    }
}